package com.yxf.variousshapes3d.shapes;

import android.graphics.Color;

import com.yxf.variousshapes3d.Point;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import static android.opengl.GLES20.GL_TRIANGLES;
import static android.opengl.GLES20.GL_UNSIGNED_BYTE;
import static android.opengl.GLES20.glDrawElements;

public class Cube extends BaseShape {

    private float[] vertices;
    private FloatBuffer vertexData;
    private float size;
    private int coordinateCount;
    private ByteBuffer positionBuffer;


    public Cube(Point center, float size) {
        super(center);
        vertices = new float[(COLOR_COMPONENT_COUNT + POSITION_COMPONENT_COUNT) * 8];
        this.size = size;
        //绘制时所需顶点个数
        coordinateCount = 3 * 2 * 6;
    }

    @Override
    public void initWithoutLock() {
        float len = size / 2;
        int color;
        int i = 0;
        //顶面正方形左上角,0
        vertices[i++] = center.x - len;
        vertices[i++] = center.y + len;
        vertices[i++] = center.z + len;
        color = Color.BLUE;
        vertices[i++] = Color.red(color) / 255f;
        vertices[i++] = Color.green(color) / 255f;
        vertices[i++] = Color.blue(color) / 255f;
        vertices[i++] = Color.alpha(color) / 255f;
        //顶面正方形左下角,1
        vertices[i++] = center.x - len;
        vertices[i++] = center.y - len;
        vertices[i++] = center.z + len;
        color = Color.RED;
        vertices[i++] = Color.red(color) / 255f;
        vertices[i++] = Color.green(color) / 255f;
        vertices[i++] = Color.blue(color) / 255f;
        vertices[i++] = Color.alpha(color) / 255f;
        //顶面正方形右下角,2
        vertices[i++] = center.x + len;
        vertices[i++] = center.y - len;
        vertices[i++] = center.z + len;
        color = Color.YELLOW;
        vertices[i++] = Color.red(color) / 255f;
        vertices[i++] = Color.green(color) / 255f;
        vertices[i++] = Color.blue(color) / 255f;
        vertices[i++] = Color.alpha(color) / 255f;
        //顶面正方形右上角,3
        vertices[i++] = center.x + len;
        vertices[i++] = center.y + len;
        vertices[i++] = center.z + len;
        color = Color.GREEN;
        vertices[i++] = Color.red(color) / 255f;
        vertices[i++] = Color.green(color) / 255f;
        vertices[i++] = Color.blue(color) / 255f;
        vertices[i++] = Color.alpha(color) / 255f;
        //底面正方形左上角,4
        vertices[i++] = center.x - len;
        vertices[i++] = center.y + len;
        vertices[i++] = center.z - len;
        color = Color.GRAY;
        vertices[i++] = Color.red(color) / 255f;
        vertices[i++] = Color.green(color) / 255f;
        vertices[i++] = Color.blue(color) / 255f;
        vertices[i++] = Color.alpha(color) / 255f;
        //底面正方形左下角,5
        vertices[i++] = center.x - len;
        vertices[i++] = center.y - len;
        vertices[i++] = center.z - len;
        color = Color.CYAN;
        vertices[i++] = Color.red(color) / 255f;
        vertices[i++] = Color.green(color) / 255f;
        vertices[i++] = Color.blue(color) / 255f;
        vertices[i++] = Color.alpha(color) / 255f;
        //底面正方形右下角,6
        vertices[i++] = center.x + len;
        vertices[i++] = center.y - len;
        vertices[i++] = center.z - len;
        color = Color.GREEN;
        vertices[i++] = Color.red(color) / 255f;
        vertices[i++] = Color.green(color) / 255f;
        vertices[i++] = Color.blue(color) / 255f;
        vertices[i++] = Color.alpha(color) / 255f;
        //底面正方形右上角,7
        vertices[i++] = center.x + len;
        vertices[i++] = center.y + len;
        vertices[i++] = center.z - len;
        color = Color.CYAN;
        vertices[i++] = Color.red(color) / 255f;
        vertices[i++] = Color.green(color) / 255f;
        vertices[i++] = Color.blue(color) / 255f;
        vertices[i++] = Color.alpha(color) / 255f;
        vertexData = encodeVertices(vertices);

        positionBuffer = ByteBuffer.allocateDirect(coordinateCount)
                .put(new byte[]{
                        //顶正方形
                        0, 1, 2,
                        0, 2, 3,
                        //底正方形
                        7, 6, 5,
                        7, 5, 4,
                        //前正方形
                        1, 5, 6,
                        1, 6, 2,
                        //后正方形
                        3, 7, 4,
                        3, 4, 0,
                        //左正方形
                        0, 4, 5,
                        0, 5, 1,
                        //右正方形
                        2, 6, 7,
                        2, 7, 3,
                });
        positionBuffer.position(0);
    }

    @Override
    protected FloatBuffer getVertexData() {
        return vertexData;
    }

    @Override
    protected void drawArrays() {
        glDrawElements(GL_TRIANGLES, coordinateCount, GL_UNSIGNED_BYTE, positionBuffer);
    }

    @Override
    protected void preDraw() {

    }

    @Override
    protected void afterDraw() {

    }
}
